package com.olddriver.crm.web.controller;


import com.olddriver.crm.annotation.PermissionName;
import com.olddriver.crm.domain.Claims;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.ClaimsQueryObject;
import com.olddriver.crm.service.IAccidentBillItemService;
import com.olddriver.crm.service.IClaimsService;
import com.olddriver.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("claims")
public class ClaimsController {

    @Autowired
    private IClaimsService claimsService;
    @Autowired
    private IAccidentBillItemService accidentBillItemService;

    @RequestMapping("list")
    @RequiresPermissions("claims:list")
    @PermissionName("查看理赔")
    public String list() {
        return "claims/list";
    }

    @ResponseBody
    @RequestMapping("query")
    @RequiresPermissions("claims:query")
    @PermissionName("理赔列表")
    public PageResult query(ClaimsQueryObject qo) {
        PageResult result = claimsService.query(qo);
        return result;
    }

   @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions("claims:saveOrUpdate")
    @PermissionName("理赔编辑")
    public JsonResult saveOrUpdate(Claims clamis) {
        Long id = clamis.getId();
        try {
            if (id != null) {
                claimsService.updateByPrimaryKey(clamis);
            } else {
                clamis.setState(1);  //初始化设置状态为1
                claimsService.insert(clamis);
            }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

   @ResponseBody
    @RequestMapping("receiptsVerify")
    @RequiresPermissions("claims:receiptsVerify")
    @PermissionName("保单有效性验证")
   //根据订单号查询保单:不存在,存在后再查保单的保期
    public JsonResult receiptsVerify(Long id,String insuranceNum) {
        try {
            claimsService.receiptsVerify(id,insuranceNum);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }
    //报案受理
    @ResponseBody
    @RequestMapping("accept")
    @RequiresPermissions("claims:accept")
    public JsonResult accept(Long id) {
        try {
                Claims claim = claimsService.selectByPrimaryKey(id);
                if (claim.getState() < 3){
                    return JsonResult.diyFaliure("保单无效,请购买或续保");
                }else {
                    claimsService.accept(id);
                    return JsonResult.diySuccess("保单受理成功");
                }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }


}
